/* eslint-disable func-names */

const fs = require("fs");
const archiver = require("archiver");
const path = require("path");
// const config = require('./config')
const ora = require("ora");

async function zipFn(filePath, fileName) {
  return new Promise((resolve, reject) => {
    const fullPath = path.join(filePath, fileName);
    const spinner = ora("开始压缩...\n");
    spinner.start();
    const archive = archiver("zip", {
      zlib: { level: 9 }, // Sets the compression level.
    });
    archive.directory(fullPath, fileName);
    const output = fs.createWriteStream(`${fullPath}.zip`);
    output.on("close", () => {
      spinner.stop();
      console.log(
        `\n ${fileName}.zip 打包完成, ${(
          archive.pointer() /
          1024 /
          1024
        ).toFixed(1)}M total bytes`
      );
      resolve();
    });
    archive.on("warning", function (err) {
      console.log(err);
    });
    archive.on("error", function (err) {
      console.log(err);
      process.exit(1);
    });
    archive.on("finish", function () {
      console.log("Data has been drained");
    });
    archive.pipe(output);
    archive.finalize();
  });
}


const filePath = path.resolve(__dirname, '../')
const fileName = 'businessPlatFormTest'

zipFn(filePath, fileName)